package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.proof.wallet.subview

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

interface IProofGhanaWalletSubmitDialogListener {
    fun onClick(type: Int)
}

class ProofGhanaWalletSubmitDialog(var onItemClick: IProofGhanaWalletSubmitDialogListener? = null) :
    DialogFragment() {

    private lateinit var list: RecyclerView
    private lateinit var confirm: TextView
    private lateinit var close: ImageView
    private lateinit var adapter: ProofGhanaWalletSubmitDialogAdapter

    private var walletList: MutableList<JSONObject> = mutableListOf()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_ghana_proof_wallet_submit_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        confirm = view.findViewById(R.id.confirm)
        list = view.findViewById(R.id.list)
        close = view.findViewById(R.id.close)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        adapter.notifyDataSetChanged()
    }

    private fun initializeView() {
        confirm.setOnClickListener {
            onItemClick?.onClick(type = 1)
        }

        close.setOnClickListener {
            dismiss()
        }

        adapter = ProofGhanaWalletSubmitDialogAdapter(walletList)
        list.layoutManager = LinearLayoutManager(context)
        list.adapter = adapter
    }

    fun setWalletList(list: List<JSONObject>?) {
        walletList.clear()
        if (list != null) {
            walletList.addAll(list)
        }
    }
}

class ProofGhanaWalletSubmitDialogAdapter(
    var dataList: List<JSONObject>
) :
    RecyclerView.Adapter<ProofGhanaWalletSubmitDialogAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_ghana_proof_wallet_submit_dialog_adapter, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val icon: ImageView = view.findViewById(R.id.icon)
        private val type: TextView = view.findViewById(R.id.type)
        private val account: TextView = view.findViewById(R.id.account)

        @SuppressLint("SetTextI18n")
        fun bindData(item: JSONObject, position: Int) {
            type.text = item.optString("walletDesc")
            account.text = item.optString("walletMobile")

            val logo = item.optString("walletLogo")
            if (logo.isNotBlank()) {
                icon.visibility = View.VISIBLE
                Glide.with(icon).load(logo).into(icon)
            } else {
                icon.visibility = View.GONE
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: JSONObject = dataList[position]
        holder.bindData(data, position)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}